package com.movie.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.movie.bean.MovieTicket;
import com.movie.bean.MovieTicketList;
import com.movie.bean.PointList;

/**
 * 09-影片排期购 票
 * 
 * 
 */
public class BuyTicketAct extends BaseActivity {

	private Context mContext = BuyTicketAct.this;
	private PointList mPointList = new PointList();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.buyticketact);
		printTable(mPointList);
		initMovieTickets();
		initBackTitleBar();
		onclick();
		// showSeatPositions(10, 12);
	}

	private ImageView yingyuanxiangqing = null;
	private ImageView duihuangquan = null;

	private TextView goseatselected = null;

	private void onclick() {
		goseatselected = (TextView) findViewById(R.id.goseatselected);
		goseatselected.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(mContext, SeatSelectedAct.class);
				mContext.startActivity(intent);
			}
		});
		yingyuanxiangqing = (ImageView) findViewById(R.id.yingyuanxiangqing);
		yingyuanxiangqing.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, CinemaDesAct.class);
				mContext.startActivity(intent);
			}
		});
		duihuangquan = (ImageView) findViewById(R.id.duihuangquan);
		duihuangquan.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
			}
		});
		onDateClick();
	}

	private int dateIndex = 0;
	private String[] dateArray = new String[] { "1月12日(今)", "1月13日(明)",
			"1月14日(后)" };
	private ImageView jian = null;
	private ImageView jia = null;
	private TextView zhangshu = null;

	private void onDateClick() {

		jian = (ImageView) findViewById(R.id.jian);
		jia = (ImageView) findViewById(R.id.jia);
		zhangshu = (TextView) findViewById(R.id.zhangshu);

		jia.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				addLogic();
			}

		});
		;
		jian.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				decreaseLogic();
			}

		});
		setZhangshu();
	}

	private void decreaseLogic() {
		if (dateIndex > 0) {
			dateIndex -= 1;
			zhangshu.setText(dateArray[dateIndex] + "");
			if (dateIndex == 0) {
				jia.setVisibility(View.VISIBLE);
				jian.setVisibility(View.GONE);
			} else if (dateIndex < 2) {
				jia.setVisibility(View.VISIBLE);
			}
		}
	}

	private void setZhangshu() {
		if (dateIndex == 0) {
			jia.setVisibility(View.VISIBLE);
			jian.setVisibility(View.GONE);
		} else if (dateIndex == 2) {
			jia.setVisibility(View.GONE);
			jian.setVisibility(View.VISIBLE);
		}
	}

	private void addLogic() {
		if (dateIndex < 2) {
			dateIndex += 1;
			zhangshu.setText(dateArray[dateIndex]);
			if (dateIndex == 2) {
				jia.setVisibility(View.GONE);
				jian.setVisibility(View.VISIBLE);
			} else if (dateIndex > 0) {
				jian.setVisibility(View.VISIBLE);
			}
			// jian.setBackgroundResource(backgrounds[1]);
			// jian.setEnabled(false);
		}
	}

	// -------------------电影票----------START
	private void initMovieTickets() {
		movie_listview = (ListView) findViewById(R.id.movie_listview);
		mMovieTicketList = new MovieTicketList();
		MovieTicket mMovieTicket = null;
		for (int i = 0; i < 10; i++) {
			mMovieTicket = new MovieTicket();
			mMovieTicket.time = "11:0" + i;
			mMovieTicket.type = "国语" + i;
			mMovieTicket.price = i + "元";
			mMovieTicket.movitype = "3D";
			mMovieTicketList.addMovieTicket(mMovieTicket);
		}
		mMovieTicketListAdapter = new MovieTicketListAdapter();
		movie_listview.setAdapter(mMovieTicketListAdapter);
		movie_listview.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> mAdapterView,
					View parent, int position, long id) {
				selected = position;
				mMovieTicketListAdapter.notifyDataSetChanged();
			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		movie_listview.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> mAdapterView, View parent,
					int position, long id) {

			}
		});

	}

	private int selected = -1;
	private boolean ismListViewHidden = true;
	private ListView movie_listview = null;
	private MovieTicketListAdapter mMovieTicketListAdapter = null;
	private MovieTicketList mMovieTicketList = null;

	public class MovieTicketListAdapter extends BaseAdapter {

		private LayoutInflater layoutFlater;

		public MovieTicketListAdapter() {
			super();
			layoutFlater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public int getCount() {
			return mMovieTicketList.mMovieTickets.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			final ViewHolderListViewItme holder;
			if (convertView == null) {
				convertView = layoutFlater.inflate(
						R.layout.movie_ticket_list_item, null);
				holder = new ViewHolderListViewItme();
				holder.time = (TextView) convertView.findViewById(R.id.time);
				holder.type = (TextView) convertView.findViewById(R.id.type);
				holder.price = (TextView) convertView.findViewById(R.id.price);
				holder.movitype = (TextView) convertView
						.findViewById(R.id.movitype);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolderListViewItme) convertView.getTag();
			}
			MovieTicket bean = mMovieTicketList.mMovieTickets.get(position);
			holder.time.setText(bean.time);
			holder.type.setText(bean.type);
			holder.price.setText(bean.price);
			holder.movitype.setText(bean.movitype);

			return convertView;
		}
	}

	private static class ViewHolderListViewItme {
		public TextView time = null;
		public TextView type = null;
		public TextView price = null;
		public TextView movitype = null;
	}

	// -------------------电影票-----------END

	// -------------------座位-----------START
	private TextView tikcetseatNum = null;
	private TextView[] tikcetseatNumTemp = null;
	private ImageView tikcetseatSeat = null;
	private LinearLayout seat_lable = null;
	private LinearLayout seat_rows = null;
	private LinearLayout seat_row_L = null;

	// 表头动态容器

	// 表内容动太容器
	private LinearLayout seat_lable_pl = null;
	private LinearLayout seat_row_pl = null;

	// private void showSeatPositions(int rowNum, int columnNum) {
	// Log.i("----------BuyTicketAct.showSeatPositions--------rowNum" + rowNum
	// + "columnNum" + columnNum);
	//
	// LayoutInflater layoutFlater = (LayoutInflater) mContext
	// .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	// seat_lable = (LinearLayout) findViewById(R.id.seat_lable);
	// seat_rows = (LinearLayout) findViewById(R.id.seat_rows);
	// if (columnNum > 4) {
	// // 生成表头-------------------------S
	//
	// // 找出表头动态容器
	// seat_lable_pl = (LinearLayout) findViewById(R.id.seat_lable_pl);
	// seat_lable_pl.removeAllViews();
	// int needNum = columnNum - 5 + 1;
	// int idleNum = 0;
	// tikcetseatNumTemp = new TextView[needNum];
	// for (int i = 5; i <= columnNum; i++) {
	// // 生成"1"
	// tikcetseatNum = (TextView) layoutFlater.inflate(
	// R.layout.ticketseat_lable_num, null);
	//
	// Log.i("----------tikcetseatNum--------i=" + i);
	// tikcetseatNum.setText(i + "");
	// tikcetseatNum.setLayoutParams(new LinearLayout.LayoutParams(20,
	// 20));
	// // 5放在最后一个12-5+1 1
	// if (i % 2 != 0) {
	// tikcetseatNumTemp[needNum - idleNum - 1] = tikcetseatNum;
	// } else {
	// tikcetseatNumTemp[idleNum] = tikcetseatNum;
	// idleNum += 1;
	// }
	// }
	// for (int i = 1; i <= needNum; i++) {
	// seat_lable_pl.addView(tikcetseatNumTemp[i - 1]);
	// }
	// // 生成表头-------------------------E
	//
	// // 生成表格-------------------------S
	//
	// // 找出表格动态容器
	// // seat_lable_pl = (LinearLayout) findViewById(R.id.seat_lable_pl);
	// // seat_lable_pl.removeAllViews();
	// // int needNum = columnNum - 5 + 1;
	// // int idleNum = 0;
	// // tikcetseatNumTemp = new TextView[needNum];
	// for (int i = 1; i <= columnNum; i++) {
	// // 生成"1"
	// seat_row_L = (LinearLayout) layoutFlater.inflate(
	// R.layout.ticketseat_row, null);
	//
	// Log.i("----------tikcetseatNum--------i=" + i);
	// // tikcetseatNum.setText(i + "tikcetseatNum");
	// // tikcetseatNum.setTextColor(R.color.seat_num);
	// // // 5放在最后一个12-5+1 1
	// // if (i % 2 != 0) {
	// // tikcetseatNumTemp[needNum - idleNum - 1] = tikcetseatNum;
	// // } else {
	// // tikcetseatNumTemp[idleNum] = tikcetseatNum;
	// // idleNum += 1;
	// // }
	// seat_rows.addView(seat_row_L);
	// }
	// // for (int i = 1; i <= needNum; i++) {
	// // seat_lable_pl.addView(tikcetseatNumTemp[i - 1]);
	// // }
	//
	// // 生成表格-------------------------E
	// }

	// }
	//
	// // -------------------座位-----------END
	// public void release() {
	// if (seat_lable != null) {
	// seat_lable.removeAllViews();
	// seat_lable = null;
	// }
	// ;
	// if (seat_rows != null) {
	// seat_rows.removeAllViews();
	// seat_rows = null;
	// }
	// ;
	// }

}
